package com.michaelbulava.TFS.ui.wizards.share;

import com.intellij.ide.wizard.CommitStepException;
import com.intellij.openapi.module.Module;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.ui.controls.vc.ServerItemTreeControl;
import com.michaelbulava.TFS.ui.controls.vc.ServerPathSelectionControl;
import com.michaelbulava.TFS.ui.framework.wizard.ExtendedWizardStep;
import com.michaelbulava.TFS.ui.wizards.common.IConnectWizard;
import com.michaelbulava.TFS.ui.wizards.common.IShareWizard;
import com.michaelbulava.TFS.vc.tfsitem.TFSItem;
import com.microsoft.tfs.core.clients.versioncontrol.path.ServerPath;
import com.microsoft.tfs.util.valid.ValidityChangedEvent;
import com.microsoft.tfs.util.valid.ValidityChangedListener;
import org.apache.http.util.TextUtils;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import java.awt.*;
import java.text.MessageFormat;

/**
 * Created by MichaelA on 7/23/2015.
 */
public class MapProjectToTFSWizardStep extends ExtendedWizardStep {
    public static final Object STEP_ID = new Object();
    private final IShareWizard parent;

    //private String mapToPath = null;
    private SelectPathControl control;

    public MapProjectToTFSWizardStep(IShareWizard wizard){
        super(Messages.getString("MapToTFSPathWizardStep.Title"));
        this.parent = wizard;
    }


    @Override
    public void _init(){
        control = new SelectPathControl();
        control.serverPathTree.setWorkspace(parent.getWorkspace());
    }

    @Override
    public Object getStepId() {
        return STEP_ID;
    }

    @Override
    public Object getNextStepId() {
        return parent.getNextStepId(this);
    }

    @Override
    public Object getPreviousStepId() {
        return parent.getPreviousStepId(this);
    }

    @Override
    public void commit(CommitType commitType) throws CommitStepException {
        if (commitType == CommitType.Next || commitType == CommitType.Finish)
            parent.setProjectShareLocation(control.serverPathTree.getSelectedPath());
    }

    @Override
    public boolean isComplete() {
        //return mapToPath != null && mapToPath.length() > 0 && !mapToPath.equals(ServerPath.ROOT);
        return control.serverPathTree.getValidator().getValidity().isValid();
    }

    @Override
    public JComponent getComponent() {
        return control;
    }

    @Override
    public JComponent getPreferredFocusedComponent() {
        return null;
    }

//    private void pathSelected(String selectedPath){
//        mapToPath = selectedPath;
//        fireStateChanged();
//    }


    private class SelectPathControl extends JPanel{
        private JTextPane locationMessageTextPane;
        private ServerPathSelectionControl serverPathTree;

        public SelectPathControl(){
            super();
            setLayout(new GridLayoutManager(2, 1, new Insets(4, 4, 4, 4), -1, -1));

            final JPanel panel2 = new JPanel();
            panel2.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
            add(panel2, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
                    GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                    GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0,
                    false));

            locationMessageTextPane = new JTextPane();
            panel2.add(locationMessageTextPane, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
                    GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_WANT_GROW,
                    GridConstraints.SIZEPOLICY_WANT_GROW, null, new Dimension(150, 50), null, 0, false));
            locationMessageTextPane.setText(MessageFormat.format(
                    Messages.getString("MapToTFSPathWizardStep.SelectPathControl.EntireProjectMessageFormat"),
                    new Object[]{parent.getProject().getName()}));


            final JBScrollPane scrollPane1 = new JBScrollPane();
            add(scrollPane1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
                    GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW,
                    GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0,
                    false));

            serverPathTree = new ServerPathSelectionControl(parent.getProject().getName());
            scrollPane1.setViewportView(serverPathTree);
            //serverPathTree.setProject(parent.getProject());
//            serverPathTree.addChangeListener(new ChangeListener() {
//                @Override
//                public void stateChanged(ChangeEvent e) {
//                    if (serverPathTree.getValidator().)
//                    fireStateChanged();
//                }
//            });
            serverPathTree.getValidator().addValidityChangedListener(new ValidityChangedListener() {
                @Override
                public void validityChanged(ValidityChangedEvent event) {
                    fireStateChanged();
                }
            });
        }
    }
}
